/*
 * @Author: Sam
 * @Date: 2021-03-01 17:33:32
 * @LastEditTime: 2021-04-13 09:44:36
 * @LastEditors: Sam
 * @Description:
 * @FilePath: \resourcesf:\thework\tofba-client\src\utils\waybill-public.js
 * @可以输入预定的版权声明、个性签名、空行等
 */
import { Message } from 'element-ui'
const offAccountCurrencyRateMap = {}

// 空对象检测
export function isNoEmptyObject (target) {
  const type = typeof target
  return target !== null && type === 'object' && Object.keys(target).length > 0
}

/**
 * 空行检测
 * @param rows
 * @returns {Boolean}
 */
export function checkRowsEmpty (rows) {

  if (rows === null) {
    return false;
  }

  for (const i in rows) {
    if (rows[i] !== null && rows[i] !== '') {
      console.log('rows[i] :>> ', rows[i]);
      return true;
    }
  }

  return false;
}
/**
 * 
 * @param currency 申报币种
 * @param invoice 箱单发票
 * @param type 0=> 判断申报货值是否大于采购货值,1=> 低货值申报
 * @returns
 */
export function CompareDeclaredValue ({ currency, invoice, type }) {
  if (!currency) {
    Message({
      message: '请选择申报币种',
      type: 'error'
    })
    return false;
  }
  if (!isNoEmptyObject(offAccountCurrencyRateMap)) {
    Message({
      message: '销帐汇率获取失败，请刷新重试',
      type: 'error'
    })
    return false;
  };
  if (!offAccountCurrencyRateMap['RMB']) {
    Message({
      message: `当前申报币种：${currency}，销帐汇率尚未配置，请先配置`,
      type: 'error'
    })
    return false;
  };
  const offAccountCurrencyRate = offAccountCurrencyRateMap['RMB'];
  if (!isNoEmptyObject(offAccountCurrencyRate)) {
    Message({
      message: `当前申报币种：${currency}，销帐汇率尚未配置，请先配置`,
      type: 'error'
    })
    return false;
  };

  if (!offAccountCurrencyRate[currency]) {
    Message({
      message: `当前申报币种：${currency}，与RMB兑换汇率尚未配置，请先配置`,
      type: 'error'
    });
    return false;
  };
  const currencyRateRateMap = offAccountCurrencyRate[currency];
  const rate = parseFloat(currencyRateRateMap['rate']);
  const operator = currencyRateRateMap['operator'];// operator
  if (operator === null || operator === undefined || operator === '') {
    operator = '1';
  };
  if (invoice.length === 0) {
    Message({
      message: '请填写清关发票',
      type: 'error'
    })
    return false;
  };
  let totalDeclareValue = 0;
  let totalPurchasingPrice = 0;
  let breakFlag = false;

  for (let [i, v] of invoice.entries()) {

    const num = v.num;
    const declareValue = v.declareValue;
    const purchasingPrice = v.purchasingPrice;
    if (!num) {
      Message({
        message: `请填写第${i + 1}行，SKU个数`,
        type: 'error'
      })
      breakFlag = true;
    };
    if (!declareValue) {
      Message({
        message: `请填写第${i + 1}行，申报单价`,
        type: 'error'
      })
      breakFlag = true;
    };
    if (!purchasingPrice) {
      Message({
        message: `请填写第${i + 1}行，采购单价`,
        type: 'error'
      })

      breakFlag = true;
    };
    totalDeclareValue = totalDeclareValue + (parseFloat(declareValue) * parseInt(num));
    totalPurchasingPrice = totalPurchasingPrice + (parseFloat(purchasingPrice) * parseInt(num));

    if (type === 0) {
      if (operator === '2') {
        criticalValue = (declareValue / rate);
      } else {
        criticalValue = (declareValue * rate);
      };

      if (criticalValue > purchasingPrice * 3) {
        Message({
          message: `您的申报货值大于采购货值的三倍,请检查第${i + 1}行`,
          type: 'error'
        })
        return false;
      }
    }

  }
  if (breakFlag) {
    return false;
  };
  if (totalPurchasingPrice > 0 && type === 1) {
    let criticalValue;
    if (operator === '2') {
      criticalValue = (totalDeclareValue / rate) / totalPurchasingPrice;
    } else {
      criticalValue = (totalDeclareValue * rate) / totalPurchasingPrice;
    };
    if (criticalValue <= 0.4) {
      Message({
        message: '因申报货值过低，运单不能保存。我司规定：申报货值不得低于采购货值的40%，否则我司不能承接此票运单！',
        type: 'error'
      })
      return false;
    };
  };
  return true;
};

/**
* 1:强制买保险;2:单箱不接收高货值;3:校验未通过
* 
* @param invoice 
* @param packingListSet
* @param customerActualWeight
*            客户下单实重
* @returns {Number}
*/
function forcedPurchaseInsurance (invoice, packingListSet, customerActualWeight, customerTotalPackages) {
  let checkResult = 0; // 异常错误
  if (invoice.length === 0) {
    Message({
      message: '请填写清关发票！',
      type: 'error'
    })
    checkResult = 1;
    return checkResult;
  };
  if (!customerActualWeight || !(customerActualWeight > 0)) {
    Message({
      message: '请填写实重！',
      type: 'error'
    })
    checkResult = 1;
    return checkResult;
  };
  if (!customerTotalPackages || customerTotalPackages === 0) {
    Message({
      message: '请填写件数！',
      type: 'error'
    })
    checkResult = 1;
    return checkResult;
  };
  let breakFlag = false;
  let totalPurchasingPrice = 0;
  let skuPurchasingPrice = {}; // 装箱单中SKU采购单价
  for (let [i, v] of invoice.entries()) {
    const num = v.num;
    const purchasingPrice = v.purchasingPrice;
    const sku = v.sku;
    if (!sku) {
      Message({
        message: `请填写第${i + 1}行，SKU信息`,
        type: 'error'
      })
      breakFlag = true;
    };
    if (!num) {
      Message({
        message: `请填写第${i + 1}行，SKU个数`,
        type: 'error'
      })
      breakFlag = true;
    };
    if (!purchasingPrice) {
      Message({
        message: `请填写第${i + 1}行，采购单价`,
        type: 'error'
      })

      breakFlag = true;
    };

    skuPurchasingPrice[sku] = purchasingPrice;
    try {
      totalPurchasingPrice = totalPurchasingPrice + (parseFloat(purchasingPrice) * parseInt(num));
    } catch (err) {
      Message({
        message: `发票第${i + 1}行，采购单价与数量做乘积运算发生异常`,
        type: 'error'
      })
      breakFlag = true;
    };
  };
  if (breakFlag) {
    checkResult = 1;
    return checkResult;
  };
  // 总采购货值/实重
  const purchasing_price_weight_ratio = totalPurchasingPrice / customerActualWeight;
  if (purchasing_price_weight_ratio > 1000) {
    checkResult = 2; // 高货值产品需客户自行投保,系统将按照采购货值的0.4%收取保费。
    return checkResult;
  };
  return checkResult;
};

function isChina (s) {
  const patrn = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
  if (!patrn.exec(s)) {
    return false;
  } else {
    return true;
  };
};
